//
//  HDItemViewController.m
//  HDTianjinNature
//
//  Created by Liuzhuan on 13-11-1.
//  Copyright (c) 2013年 hengdawb. All rights reserved.
//

#import "HDItemViewController.h"

@interface HDItemViewController ()
@property (nonatomic, strong) NavigationTitleView *myTitleLabel;
@end

@implementation HDItemViewController

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(5, 15, 40, 40);
    leftButton.imageView.contentMode = UIViewContentModeScaleToFill;
    [leftButton setImage:[UIImage imageNamed:@"navi_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    [self.navigationItem setLeftBarButtonItem:leftBarButtonItem];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0.0, 40, 40);
    [rightButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [self.navigationItem setRightBarButtonItem:rightBarButtonItem];
    [self titleLabeInit];
    
    NSString *barImageName = @"navi_bar";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:barImageName] forBarMetrics:UIBarMetricsDefault];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//白色
}

- (void)setTitle:(NSString *)title
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self titleLabeInit];
        NSString *myTitle = title;
        if (title.length == 0)
            myTitle = @" ";
        _myTitleLabel.titleName = myTitle;
        _myTitleLabel.frame = CGRectMake(50, 0, ScreenWidth-100, 44);
        _myTitleLabel.titleFont = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:22.0];
        _myTitleLabel.titleColor = Text_Common_Color;
        _myTitleLabel.backgroundColor = [UIColor clearColor];
        NSLog(@" frame is %@", [NSValue valueWithCGRect:_myTitleLabel.frame]);
        
    });
}

- (void)titleLabeInit
{
    [self titleRelease];
    _myTitleLabel = [[NavigationTitleView alloc]init];
    _myTitleLabel.frame = CGRectMake(50, 0, ScreenWidth-100, 44);
    _myTitleLabel.titleName = @"  ";
    
    self.navigationItem.titleView = _myTitleLabel;
}

- (void)titleRelease
{
    if (_myTitleLabel != nil)
    {
        self.myTitleLabel = nil;
    }
}

- (void)back
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self titleRelease];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

    NSMutableDictionary *attributes= [[NSMutableDictionary alloc] init];
    [attributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [attributes setValue:[UIColor clearColor] forKey:NSBackgroundColorAttributeName];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    NSString *barImageName = @"navi_bar";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:barImageName] forBarMetrics:UIBarMetricsDefault];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
